using System;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace RxjhServer
{
	public class Hasher
	{
		public struct CPU_INFO
		{
			public uint dwOemId;

			public uint dwPageSize;

			public uint lpMinimumApplicationAddress;

			public uint lpMaximumApplicationAddress;

			public uint dwActiveProcessorMask;

			public uint dwNumberOfProcessors;

			public uint dwProcessorType;

			public uint dwAllocationGranularity;

			public uint dwProcessorLevel;

			public uint dwProcessorRevision;
		}

		[DllImport("kernel32")]
		public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);

		public static string GetCpuID()
		{
			try
			{
				ManagementClass managementClass = new ManagementClass("Win32_Processor");
				ManagementObjectCollection instances = managementClass.GetInstances();
				string result = null;
				using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = instances.GetEnumerator())
				{
					if (managementObjectEnumerator.MoveNext())
					{
						ManagementObject managementObject = (ManagementObject)managementObjectEnumerator.Current;
						result = managementObject.Properties["ProcessorId"].Value.ToString();
					}
				}
				return result;
			}
			catch (Exception)
			{
				return "CpUIDerror";
			}
		}

		public static string GetIP()
		{
			IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
			IPAddress[] addressList = hostEntry.AddressList;
			for (int i = 0; i < addressList.Length; i++)
			{
				if (addressList[i].AddressFamily == AddressFamily.InterNetwork)
				{
					return addressList[i].ToString();
				}
			}
			return addressList[0].ToString();
		}

		public static string GetMac()
		{
			string result = string.Empty;
			try
			{
				ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
				ManagementObjectCollection instances = managementClass.GetInstances();
				foreach (ManagementObject item in instances)
				{
					if (item["IPEnabled"].ToString() == "True")
					{
						result = item["MacAddress"].ToString();
					}
				}
			}
			catch
			{
				result = string.Empty;
			}
			return result;
		}

		[DllImport("kernel32.dll")]
		private static extern int GetVolumeInformation(string lpRootPathName, string lpVolumeNameBuffer, int nVolumeNameSize, ref int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize);

		public static string GetDriveID(string drvID)
		{
			try
			{
				ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
				string result = null;
				using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = managementObjectSearcher.Get().GetEnumerator())
				{
					if (managementObjectEnumerator.MoveNext())
					{
						ManagementObject managementObject = (ManagementObject)managementObjectEnumerator.Current;
						result = managementObject["SerialNumber"].ToString().Trim();
					}
				}
				return result;
			}
			catch
			{
				return "0";
			}
		}
	}
}
